import request from './request';

// 设置基本的opthions
function setOptions(method = 'GET', param) {
  const token = sessionStorage.getItem('token');
  const options = {
    method: method,
    headers: {
      'Content-Type': 'application/json',
      Authorization: token ? token : '',
    },
    handleAs: 'json',
  };
  if (param) {
    options.body = JSON.stringify(param);
  }
  return options;
}

export function userLogin(username, password) {
  return request(window.appcfg.servicesUrl + '/login/login', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    handleAs: 'json',
    body: JSON.stringify({ username, password }),
  });
}

// 获取所有的模型信息
export function getBimType() {
  const options = setOptions('GET');
  return request(window.appcfg.servicesUrl + '/property/getBimType', options);
}

export function getBimTree() {
  const options = setOptions('GET');
  return request(window.appcfg.servicesUrl + '/bMenu/getTree', options);
}

// 展示bim模型目录树
const baseUrl = window.appcfg._servicesUrl;
// 获取slpk bim目录树一级菜单
export async function getSlpkBimDirectory(mark, level, parent = '', grand = '') {
  const options = setOptions('GET');
  return request(
    `${baseUrl ||
      window.appcfg
        .servicesUrl}/slpk/getDirectory?mark=${mark}&level=${level}&fatherName=${parent ||
      '0'}&grandfatherName=${grand || '0'}`,
    options,
  ).then(data => {
    // api修改后套一层数据
    return { data };
  });
}

// 获取slpk bim目录树各级子菜单,异步加载树
export async function getSlpkBimSubDirectory(mark, parent, grand = '', level = 2) {
  let result = [];
  try {
    const { data: _data } = await getSlpkBimDirectory(mark, level, parent, grand || parent);
    result = _data.data.map(d => {
      return { level, parent, name: d, children: [] };
    });
  } catch (error) {}
  return result;
}

// 获取slpk bim目录树一级菜单下所有子菜单的名称
export function getSlpkBimAllchilds(mark, firstname) {
  const options = setOptions('GET');
  return request(
    `${baseUrl || window.appcfg.servicesUrl}/slpk/findByName/${mark}/${firstname}`,
    options,
  ).then(data => {
    // api修改后套一层数据
    return { data };
  });
}

// 根据objectid来获取slpk bim 构件的坐标位置
export function getSlpkBimCoordinate(mark, objectid) {
  const options = setOptions('GET');
  return request(
    `${baseUrl || window.appcfg.servicesUrl}/slpk/findCoordinateList/${mark}/${objectid}`,
    options,
  ).then(data => {
    // api修改后套一层数据
    return { data };
  });
}

// 根据时间区间获取施工模拟数据
// export async function getTaskbyInterval(param) {
//   const options = setOptions('GET');
//   return request(`${window.appcfg.servicesUrl}/task/task2/${param.mark}?startDate=${param.startdate}&endDate=${param.enddate}`, options);
// }

